<?php
namespace app\member\controller;
use app\member\controller\Base;  
class Order  extends Base
{
   public function orderlist(){
      $order = db('order');
       $uid = session('uid');
       //paginate()函数可以带三个参数：
        // $listRows  每页数量 数组表示配置参数
        // $simple   是否简洁模式或者总记录数 如果为true，那么分页的就是只有上一页和下一页
        // $config   配置参数 具体可以自己传入或者在配置文件中配置
        $orderStatus = input('order_status');
        $map = array();
        if(!$orderStatus){
            $orderStatus = 1;
        }
        if($orderStatus == 1){
         $map['id'] = ['>',1];
        }elseif($orderStatus == 2){
         $map['order_status'] = 0;
        }elseif($orderStatus == 3){
         $map['pay_status'] =0;
        }elseif($orderStatus == 4){
         $map['post_status']= 1;
        }elseif($orderStatus == 5){ 
         $map['order_status'] = 1;
      }else{
         $map['id'] = ['>',1];
      }
       $orderRes = $order->field('id,out_trade_no,user_id,order_total_price,order_status,pay_status,post_status,order_time,name')->where('del_status',0)->where($map)->where('user_id',$uid)->paginate(3,false,['query'=>request()->param()])->each(function($item, $key){
        $orderid = $item["id"]; //获取数据集中的id
        $goodsRes = db('orderGoods')->alias('og')->field('g.mid_thumb,g.goods_name,og.member_price,og.goods_attr_str,og.goods_num')->join('goods g',"g.id = og.goods_id")->where('order_id',$orderid)->select(); //根据ID查询相关其他信息
        $item['goods'] = $goodsRes; //给数据集追加字段num并赋值
        return $item;
    });
      //全部订单的数量
      $totalCount = $order->where('del_status',0)->count();
      //未完成
      $notDoneCount = $order->where(['del_status'=>0,'order_status'=>0])->count();
      //未支付
      $notPayCount = $order->where(['del_status'=>0,'pay_status'=>0])->count();
       //待收货
      $notGetCount = $order->where(['del_status'=>0,'post_status'=>1])->count();
      //已完成订单数量
      $doneCount = $order->where(['del_status'=>0,'order_status'=>1])->count();
      // echo $totalCount;
       $this->assign([
        'orderRes' => $orderRes,
        'totalCount'=>$totalCount,
        'notDoneCount'=>$notDoneCount,
        'notPayCount'=>$notPayCount,
        'notGetCount'=>$notGetCount,
        'doneCount'=>$doneCount,
        'orderStatus'=>$orderStatus
       ]);
	   return view();
   }
  



   public function orderDetail(){
      $orderId = input('id');
      if(!$orderId){
         $this->error('非法操作!');
      }

      $orders = db('order')->find($orderId);
      if(!$orders){
         $this->error('非法操作!');
      }


      //订单进度
      $progress = 1;
      if($orders['pay_status'] == 1){ //如果已支付  pay_status 0 未支付  1已支付
         $progress = 2;
            if($orders['post_status'] == 1){ //若已发货  post_status 0 未发货 1 已发货 2 已收货
               $progress = 3;
            }
               if($orders['post_status'] == 2){
                  $progress = 4;
                  if($orders['order_status'] == 1){ // 订单状态 order_status 0 未确认 1 已确认 2 申请退款 3退款成功
                     $progress = 5;
                  }
               }
      }
      //当前订单商品查询
      $goodsRes = db('orderGoods')->alias('og')->field('g.id,g.mid_thumb,g.goods_name,og.member_price,og.goods_attr_str,og.goods_num')->join('goods g',"g.id = og.goods_id")->where('order_id',$orderId)->select(); //根据ID查询相关其他信息
      // dump($goodsRes);
      $this->assign([
         'orders'=>$orders,
         'orderProgress'=>$progress,
         'goodsRes'=>$goodsRes
      ]);
      return view();
  }
  public function delOrder(){
     $orderId = input('id');
     $save = db('order')->update(['id'=>$orderId,'del_status'=>1]);
     if($save){
         $this->success('删除订单成功！');
     }else{
         $this->error('删除订单失败！');
     }
  }
}
